import { HttpService } from '@nestjs/axios';
import { BadRequestException, Controller, Get, Inject, Param } from '@nestjs/common';
import pinyin from 'pinyin';
import { firstValueFrom } from 'rxjs';

@Controller('weather')
export class WeatherController {

    @Inject(HttpService)
    private httpService:HttpService

    @Get('/:city')
    async weather(@Param('city') city: string) {
        const { data } = await firstValueFrom(
            this.httpService.get(`https://geoapi.qweather.com/v2/city/lookup?location=${city}&key=70121e8e595146b49c5960980b7f7b90`)
        )
        const location = data?.['location']?.[0];
        if (!location) {
            throw new BadRequestException('没有对应的城市信息');
        }
        const { data: weatherData } = await firstValueFrom(
            this.httpService.get(`https://devapi.qweather.com/v7/weather/now?location=${location.id}&key=70121e8e595146b49c5960980b7f7b90`)
        )
        return weatherData;
    }

}
